using System.Collections;
using Concurso;
using Curriculum.Metadatos.ClasesMetadatos;
using Curriculum.Services;

namespace ConcursoCV.UI.WebUserControls.AdmVersionesCV
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for ABMAntecedenteVersionCV.
	/// </summary>
	public class ABMAntecedenteVersionCV : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.TextBox txtNombre;
		protected System.Web.UI.WebControls.DataGrid dtgAtributos;
		protected System.Web.UI.WebControls.TextBox txtObservaciones;
		protected System.Web.UI.WebControls.DropDownList ddlAtributo;
		protected System.Web.UI.WebControls.Panel pnlAtributo;
		protected System.Web.UI.WebControls.TextBox txtLongitud;
		protected System.Web.UI.WebControls.CheckBox chkOpcional;
		protected System.Web.UI.WebControls.Button btnAgregar;
		protected System.Web.UI.WebControls.Button btnCancelarAgregar;
		protected System.Web.UI.WebControls.Button btnCancelar;
		protected System.Web.UI.WebControls.TextBox txtNombreAtributo;
		protected System.Web.UI.WebControls.Button btnAgregarAtributo;
		protected System.Web.UI.WebControls.Label lblCount;
		protected System.Web.UI.WebControls.TextBox txtTipo;
		protected Pluspetrol.Core.UIControls.WebControls.ConfirmButton btnAceptar;

		public event EventHandler Add;
		public event EventHandler Modificar;
		public event EventHandler Cancelar;
		
		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!this.IsPostBack)
			{
				InitializeDataBindings();
			}
		}

		private void InitializeDataBindings()
		{
			VersionMetadatosService svc = new VersionMetadatosService();
			IList atributos = svc.GetAllAtributos();
			Helper.ListHelper.FillDropDownList(ddlAtributo,"Id","Nombre",atributos,true);
		}


		#region - LoadMethods -

		public void CargarAntecedenteAControl(string idAntecedenteMetadato)
		{
			VersionMetadatosService svc = new VersionMetadatosService();
			AntecedenteMetadato aMetadato = svc.ObtenerAntecedenetMetadato(idAntecedenteMetadato);
			AntecedenteMetadatoId = aMetadato.Id;
			txtNombre.Text = aMetadato.Nombre;
			LlenarDataGridAtributos();			
		}
		private void CargarAtributoAControl(string idAtributoMetadato)
		{
			VersionMetadatosService svc = new VersionMetadatosService();
			AtributoMetadato atributoMetadato = svc.ObtenerAtributoMetadato(idAtributoMetadato);
			ddlAtributo.SelectedValue = atributoMetadato.Id;
			txtNombreAtributo.Text = atributoMetadato.Nombre;
			txtLongitud.Text = atributoMetadato.Longitud.ToString();
			txtTipo.Text = svc.GetTipoDatoPorId(atributoMetadato.IdTipoDato).Value;
			chkOpcional.Checked = atributoMetadato.Opcional;
			
		}

		public void CargarControlAAntecedente(AntecedenteMetadato antecedenteMetadato)
		{
			antecedenteMetadato.Id = AntecedenteMetadatoId;
			antecedenteMetadato.IdPadre = AntecedenteMetadatoPadreId;
			antecedenteMetadato.Nombre = txtNombre.Text;
		}		

		private void CargarAtributos(AntecedenteMetadato antecedenteMetadato, IList listaAtributos)
		{
			AtributoMetadato atributoMeta = null;
			if(listaAtributos.Count>0)
			{
				antecedenteMetadato.AtributoMetadatos = new AtributoMetadato[listaAtributos.Count];
				for(int i=0; i<listaAtributos.Count;i++)
				{
					atributoMeta = new AtributoMetadato();
					atributoMeta.Id = listaAtributos[i].ToString();
					antecedenteMetadato.AtributoMetadatos[i] = atributoMeta;
				}				
			}
		}
	
		#endregion - LoadMethods -

		public void Nuevo ()
		{
			AntecedenteMetadatoId = "0";
			LimpiarControles();
			EnabledDisableControls(true);
			lblCount.Text = "0";
		}
		public void Modificacion ()
		{
			EnabledDisableControls(true);
		}

		public void EnabledDisableControls(bool enabled)
		{
			txtNombre.Enabled = enabled;
			btnAgregarAtributo.Visible = enabled;
			btnAceptar.Visible = enabled;
			btnCancelar.Visible = enabled;
			dtgAtributos.Columns[2].Visible = enabled;
		}

		private void LimpiarControles()
		{
			txtNombre.Text = string.Empty;
			dtgAtributos.DataSource = new ArrayList();
			dtgAtributos.DataBind();
		}

		private void VisiblePnlAtributo(bool visible)
		{
			pnlAtributo.Visible = visible;
		}
		private void EnabledButtons(bool enabled)
		{
			btnAceptar.Enabled = enabled;
			btnCancelar.Enabled = enabled;
		}

		#region - Crear, Modificar -

		private void CrearAntecedenteMetadato(AntecedenteMetadato aMetadato)
		{
			VersionMetadatosService svc = new VersionMetadatosService();
			AntecedenteMetadatoId = (string) svc.CrearAntecedenteMetadato(aMetadato);
		}
		private void ModificarAntecedenteMetadato(AntecedenteMetadato aMetadato)
		{
			VersionMetadatosService svc = new VersionMetadatosService();
			svc.ModificarAntecedenteMetadato(aMetadato);			
		}

		#endregion - Crear, Modificar -

		private void ActualizarListaAtributos(string atributoMetadatoId)
		{
			listaAtributosViewState.Add(atributoMetadatoId);
		}
		private void Guardar()
		{
			AntecedenteMetadato antecedenteMetadato = new AntecedenteMetadato();
			CargarControlAAntecedente(antecedenteMetadato);
			CargarAtributos( antecedenteMetadato , listaAtributosViewState );
			listaAtributosViewState = null;
			if( PageMode == PageMode.NEW )
			{
				CrearAntecedenteMetadato( antecedenteMetadato );
				EnabledDisableControls( false );
				this.RaiseAdd();
			}
			else
			{
				if( PageMode == PageMode.EDIT )
				{
					ModificarAntecedenteMetadato( antecedenteMetadato );
					EnabledDisableControls( false );
					this.RaiseModificar();
				}
			}
		}		

		private void CleanControls()
		{
			txtNombreAtributo.Text = string.Empty;
			txtLongitud.Text = string.Empty;
			txtTipo.Text = string.Empty;
			chkOpcional.Checked = false;
		}

		private void LlenarDataGridAtributos()
		{
			IList list = ObtenerListaAtributos();
			lblCount.Text = list.Count.ToString();
			dtgAtributos.DataSource = list;
			try 
			{
				dtgAtributos.DataBind();
			}
			catch(Exception ex)
			{
				string msg = ex.Message;
				dtgAtributos.CurrentPageIndex=0;
				LlenarDataGridAtributos();
			}
			list = null;
		}
		private IList ObtenerListaAtributos()
		{
			IList listaAtributos = new ArrayList();
			VersionMetadatosService svc = new VersionMetadatosService();
			if( PageMode != PageMode.NEW )
			{				
				IList listaAtributosDB = svc.ObtenerAntecedenetMetadato(AntecedenteMetadatoId).AtributoMetadatos;
				foreach(AtributoMetadato atributoMetadato in listaAtributosDB)
				{
					listaAtributos.Add( atributoMetadato );
				}
			}
			foreach(object idAtributo in listaAtributosViewState )
			{
				listaAtributos.Add(svc.ObtenerAtributoMetadato(idAtributo.ToString()));
			}
			return listaAtributos;
		}

		#region - properties -
		
		public PageMode PageMode
		{
			get{ return (PageMode) ViewState["_mode"];}
			set{ ViewState["_mode"] = value;}
		}

		public IList listaAtributosViewState
		{
			get
			{
				if(ViewState["_listaAtributos"] == null)
					ViewState["_listaAtributos"] = new ArrayList();
				return (IList)ViewState["_listaAtributos"];
			}
			set{ ViewState["_listaAtributos"] = value;}			
		}
		
		public string AntecedenteMetadatoId
		{
			get{ return (string) ViewState["_antecedenteMetadatoId"];}
			set{ ViewState["_antecedenteMetadatoId"] = value;}
		}
		public string AntecedenteMetadatoPadreId
		{
			get{ return (string) ViewState["_antecedenteMetadatoPadreId"];}
			set{ ViewState["_antecedenteMetadatoPadreId"] = value;}
		}

		#endregion - properties -

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.dtgAtributos.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dtgAtributos_ItemCommand);
			this.dtgAtributos.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dtgAtributos_PageIndexChanged);
			this.btnAgregarAtributo.Click += new System.EventHandler(this.btnAgregarAtributo_Click);
			this.ddlAtributo.SelectedIndexChanged += new System.EventHandler(this.ddlAtributo_SelectedIndexChanged);
			this.btnAgregar.Click += new System.EventHandler(this.btnAgregar_Click);
			this.btnCancelarAgregar.Click += new System.EventHandler(this.btnCancelarAgregar_Click);
			this.btnAceptar.Click += new System.EventHandler(this.btnAceptar_Click);
			this.btnCancelar.Click += new System.EventHandler(this.btnCancelar_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnAgregarAtributo_Click(object sender, System.EventArgs e)
		{
			VisiblePnlAtributo(true);
			CleanControls();
			ddlAtributo.SelectedIndex = 0;
			ddlAtributo.Enabled = true;
			EnabledButtons(false);
			btnAgregar.Enabled = true;
		}
		private void btnAgregar_Click(object sender, System.EventArgs e)
		{
			ActualizarListaAtributos(ddlAtributo.SelectedValue);
			LlenarDataGridAtributos();
			VisiblePnlAtributo(false);
			EnabledButtons(true);			
		}

		private void btnCancelarAgregar_Click(object sender, System.EventArgs e)
		{
			VisiblePnlAtributo(false);
			EnabledButtons(true);
		}

		private void dtgAtributos_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if ( e.CommandName != "Page" ) 
			{
				string atributoId = e.Item.Cells[0].Text;				
				switch ( e.CommandName ) 
				{
					case "Ver":
							ddlAtributo.Enabled = false;
							btnAgregar.Enabled = false;
							CargarAtributoAControl(atributoId);
							EnabledButtons(false);
							VisiblePnlAtributo(true);
						break;
					default:
						throw new Exception("comando incorrecto");
				}
			}					
		}

		private void ddlAtributo_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(ddlAtributo.SelectedValue != "0")
			{
				CargarAtributoAControl(ddlAtributo.SelectedValue);
			}
			else
			{				
				CleanControls();
			}
		}

		private void btnAceptar_Click(object sender, System.EventArgs e)
		{
			Guardar();
		}

		private void dtgAtributos_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			dtgAtributos.CurrentPageIndex = e.NewPageIndex;
			LlenarDataGridAtributos();
		}
		
		#region - RaiseEvents -

		protected void RaiseAdd () 
		{
			if (this.Add != null)
				this.Add(this, null);		
		}
		protected void RaiseModificar () 
		{
			if (this.Modificar != null)
				this.Modificar(this, null);		
		}
		protected void RaiseCancelar () 
		{
			if (this.Cancelar != null)
				this.Cancelar(this, null);		
		}

		#endregion - RaiseEvents -

		private void btnCancelar_Click(object sender, System.EventArgs e)
		{
			EnabledDisableControls(false);
			this.RaiseCancelar();
		}
	}
}
